package com.jic.interaction.score.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.common.enums.RedisKey;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.common.utils.RedisService;
import com.jic.interaction.score.service.ScoreOrderService;
import com.jic.interaction.sign.SingUtils;
import com.jic.interaction.utils.SignUtils;
import com.jic.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Slf4j
public class ScoreOrderServiceImpl implements ScoreOrderService {
    @Autowired
    private RedisService redisService;

    @Override
    public JSONObject queryUser(JSONObject ret) {
        String signTicket = redisService.getValue(RedisKey.WZ_SING_TICKET);
        if(StringUtils.isEmpty(signTicket)){
            log.error("ticket为空!...");
            return null;
        }
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String res = String.valueOf(ts);
        param.put("appId","W3408745");
        param.put("userId",ret.getString("userId"));
        Map<String,Object> header=new HashMap<>();
        header.put("appId","W3408745");
        header.put("nonce",nonceStr);
        header.put("timestamp",res);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header,signTicket);
        String s = HttpClientUtil.doRequestWczPost("m1/dbs-front/user/queryAccountInfo?appId=W3408745" + "&nonce=" + nonceStr + "&timestamp=" + res + "&sign=" + sign + "&version=1.0.0", param);
        return JSONObject.parseObject(s);
    }

    @Override
    public JSONObject scorePay(JSONObject ret) {
        String signTicket = redisService.getValue(RedisKey.WZ_SING_TICKET);
        if(StringUtils.isEmpty(signTicket)){
            log.error("ticket为空!...");
            return null;
        }
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String res = String.valueOf(ts);
        param.put("appId","W3408745");
        param.put("userId",ret.getString("userId"));
        param.put("orderNo",ret.getString("orderNo"));
        param.put("txnTime", DateUtil.getStringFromDate(new Date(),"yyyyMMddHHmmss"));
        param.put("amount",ret.getBigDecimal("amount"));
        param.put("payAmount","0.1");
        Map<String,Object> header=new HashMap<>();
        header.put("appId","W3408745");
        header.put("nonce",nonceStr);
        header.put("timestamp",res);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header, signTicket);
        String s = HttpClientUtil.doRequestWczPost("m1/dbs-front/compensationPay/pay?appId=W3408745" + "&nonce=" + nonceStr + "&timestamp=" + res + "&sign=" + sign + "&version=1.0.0", param);
        return JSONObject.parseObject(s);
    }

    @Override
    public JSONObject scorePayQuery(JSONObject ret) {
        String signTicket = redisService.getValue(RedisKey.WZ_SING_TICKET);
        Map<String,Object> param=new HashMap<>();
        String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
        Date date = new Date(System.currentTimeMillis());
        long ts = date.getTime();
        String res = String.valueOf(ts);
        param.put("appId","W3408745");
        param.put("userId",ret.getString("userId"));
        param.put("orderNo",ret.getString("orderNo"));
        param.put("transType","P");
        Map<String,Object> header=new HashMap<>();
        header.put("appId","W3408745");
        header.put("nonce",nonceStr);
        header.put("timestamp",res);
        header.put("version","1.0.0");
        header.put("jsonData", JSON.toJSONString(param));
        //进行签名计算
        String sign = SignUtils.sign(header,signTicket);
        String s = HttpClientUtil.doRequestWczPost("m1/dbs-front/compensationPay/query?appId=W3408745" + "&nonce=" + nonceStr + "&timestamp=" + res + "&sign=" + sign + "&version=1.0.0", param);
        return JSONObject.parseObject(s);
    }
}
